/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2023. All rights reserved.
 * Description: 
 * Author: g00421808
 * Create: 1/5/2023
 * Notes: 
 */

#ifndef JSBIND_CALLBACK_BINDER_H
#define JSBIND_CALLBACK_BINDER_H
#include <functional>
#include <unordered_map>

#include "binder.h"
#include "jsbind/logging/logging.h"

namespace Jsb {
template <typename BinderType, typename R, typename... P>
class CallbackBinder : public Binder {
public:
#if JSBIND_SUPPORT_DECLARATION
    CallbackBinder()
        : Binder(type,
                 reinterpret_cast<FuncPtr>(BinderType::Wrapper),
                 sizeof...(P),
                 {})
          { }
#else
    CallbackBinder()
        : Binder(type,
                 reinterpret_cast<FuncPtr>(BinderType::Wrapper),
                 sizeof...(P))
    { }
#endif
    ~CallbackBinder() = default;
    
    template<typename... Args>
    static R InnerWrapper(std::function<R (P...)>* invoker, Args&&... args) {
        JSB_DLOG(DEBUG) << "NapiCallbackBinder::InnerWrapper";
        
        return (*invoker)(args...);
    }
    
    static constexpr auto type = Binder::Type::Func;
};
} // namespace Jsb
#endif //JSBIND_CALLBACK_BINDER_H
